package com.lzy.business.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.business.facade.OrderProductFacade;
import com.lzy.business.request.OrderProductRequest;
import com.lzy.business.response.OrderProductResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.MemberCouponsServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.MemberCouponsDto;
import com.lzy.product.dtos.OrderProductDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 卡券宝会员管理
 */
@Controller
@RequestMapping("admin/coupons/member")
public class CouponsMemberController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CouponsMemberController.class);

    @Autowired
    OrderProductFacade orderProductFacade;

    @Autowired
    MemberCouponsServiceApi memberCouponsServiceApi;

    /**
     * 会员首页
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String memberList(Model model) {
        return "product/member/member_list";
    }

    /**
     * 会员合同列表页面
     */
    @RequestMapping(value = "/contractList/{id}", method = RequestMethod.GET)
    public String contractList(@PathVariable("id") Long memberId, Model model) {
        model.addAttribute("memberId", memberId);
        return "product/member/contract_list";
    }

    /**
     * 会员补贴信息列表页面
     */
    @RequestMapping(value = "/incomeList/{id}", method = RequestMethod.GET)
    public String incomeList(@PathVariable("id") Long memberId, Model model) {
        model.addAttribute("memberId", memberId);
        return "product/member/subsidies_list";
    }

    /**
     * 会员合同审核详情页面
     */
    @RequestMapping(value = "/contractView/{id}", method = RequestMethod.GET)
    public String contractView(@PathVariable("id") Long orderId, Model model) {
        OrderProductDto dto = orderProductFacade.memberOrderProductDetail(orderId);
        model.addAttribute("dto", dto);
        return "product/member/contract_view";
    }

    /**
     * 会员合同审核详情页面
     */
    @RequestMapping(value = "/contractDetail/{id}", method = RequestMethod.GET)
    public String contractDetail(@PathVariable("id") Long orderId, Model model) {
        OrderProductDto dto = orderProductFacade.memberOrderProductDetail(orderId);
        model.addAttribute("dto", dto);
        return "product/member/contract_detail";
    }

    /**
     * 会员列表
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectMemberList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectMemberList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

    /**
     * 合同列表
     */
    @RequestMapping(value = "/selectContract", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectContractList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectMemberOrderList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

    /**
     * 补贴列表
     */
    @RequestMapping(value = "/selectIncome", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectIncomeList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectMemberIncomeList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

    /**
     * 合同审核
     */
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ResponseBody
    public Result contractVerify(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Boolean flag = orderProductFacade.verifyContract(DozerUtils.map(request,OrderProductDto.class));
        Ensure.that(flag).isTrue("08000001");
        return new Result();
    }

    /**
     * 转至购买记录页面
     */
    @RequestMapping(value = "/purchaseList/{id}", method = RequestMethod.GET)
    public String purchaseList(@PathVariable("id") Long memberId, Model model) {
        model.addAttribute("memberId", memberId);
        return "product/member/buy_history";
    }

    /**
     * 购买记录
     */
    @RequestMapping(value = "/selectPurchase", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectPurchaseList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectPurchaseList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

    /**
     * 转至购买记录页面
     */
    @RequestMapping(value = "/expenseList/{id}", method = RequestMethod.GET)
    public String expenseList(@PathVariable("id") String memberId, Model model) {
        model.addAttribute("memberId", memberId);
        return "product/member/expense_history";
    }

    /**
     * 消费记录
     */
    @RequestMapping(value = "/selectExpense", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectExpenseList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectExpenseList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

    /**
     * 转至账号信息页面
     */
    @RequestMapping(value = "/accountList/{id}", method = RequestMethod.GET)
    public String accountList(@PathVariable("id") Long memberId, Model model) {
        model.addAttribute("memberId", memberId);
        MemberCouponsDto memberCouponsDto = memberCouponsServiceApi.findByMemberId(memberId);
        if(memberCouponsDto != null){
            model.addAttribute("dto",memberCouponsDto);
        }
        return "product/member/account_detail";
    }

    /**
     * 账号信息
     */
    @RequestMapping(value = "/selectAccountList", method = RequestMethod.GET)
    @ResponseBody
    public PageBean selectAccountList(OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        request.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo<OrderProductDto> pageInfo = orderProductFacade.selectAccountList(pager, DozerUtils.map(request, OrderProductDto.class));
        if (pageInfo != null) {
            return new PageBean<>(pageInfo.getTotal(), DozerUtils.maps(pageInfo.getList(), OrderProductResponse.class));
        }
        return null;
    }

}
